package com.agile.leetcode.rpc.server;

import com.agile.leetcode.rpc.pojo.CalculatorRpcRequest;

import java.io.*;
import java.lang.reflect.Method;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Objects;

/**
 * @Author KD
 * @Date 2021/1/19 15:23
 */
public class CalculatorServer {
    public static void main(String[] args) {
        InputStream inputStream = null;
        ObjectInputStream objectInputStream = null;
        OutputStream outputStream = null;
        ObjectOutputStream objectOutputStream = null;

        try {
            // 监听端口
            ServerSocket serverSocket = new ServerSocket(7001);
            while (true) {
                // 监听客户端
                Socket socket = serverSocket.accept();
                // 拿到输入流
                inputStream = socket.getInputStream();
                // 拿到对象输入
                objectInputStream = new ObjectInputStream(inputStream);
                // 拿到对象
                Object object = objectInputStream.readObject();
                if (object instanceof CalculatorRpcRequest) {
                    CalculatorRpcRequest calculatorRpcRequest = (CalculatorRpcRequest) object;
                    // 拿到参数
                    int a = calculatorRpcRequest.getA();
                    int b = calculatorRpcRequest.getB();
                    // 拿到方法
                    String method = calculatorRpcRequest.getMethod();
                    // 拿到计算方法
                    RemoteCanculateImpl remoteCanculate = new RemoteCanculateImpl();
                    Class<? extends RemoteCanculateImpl> clazz = remoteCanculate.getClass();
                    // 拿到方法
                    Method[] methods = clazz.getMethods();
                    for (Method method1 : methods) {
                        // 拿到名称
                        String name = method1.getName();
                        if (Objects.equals(name, method)) {
                            Object invoke = method1.invoke(remoteCanculate, a, b);
                            //
                            if (invoke instanceof Integer) {
                                // 写回客户端
                                outputStream = socket.getOutputStream();
                                objectOutputStream = new ObjectOutputStream(outputStream);
                                objectOutputStream.writeObject(invoke);
                            }
                        }
                    }
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                if (objectOutputStream != null) {
                    objectInputStream.close();
                }
                if (outputStream != null) {
                    inputStream.close();
                }
                if (objectInputStream != null) {
                    objectInputStream.close();
                }
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException ex) {

            }
        }
    }

}
